/**
 * @ClassName:
 * @author:
 * @Description:
 * @Date
 **/
public class BinaryTreeMap<K extends Comparable<K>, V> implements Map<K, V> {

    private class Node {
        public K key;
        public V value;
        public Node left;
        public Node right;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            left = null;
            right = null;
        }
    }

    private Node root;
    private int size;

    public BinaryTreeMap() {
        root = null;
        size = 0;
    }

    @Override
    public void add(K key, V value) {
        add(root, key, value);
    }

    private Node add(Node root, K key, V value) {
        if (root == null) {
            size++;
            return new Node(key, value);
        }
        if (key.compareTo(root.key) < 0) {
            //接上左子树
            root.left = add(root.left, key, value);
        } else if (key.compareTo(root.key) > 0) {
            //接上右子树
            root.right = add(root.right, key, value);
        } else {
            root.value = value;
        }
        return root;
    }

    private Node getNode(Node root, K key) {
        if (root == null) {
            return null;
        }
        if (root.key.compareTo(key) == 0) {
            return root;
        } else if (key.compareTo(root.key) < 0) {
            return getNode(root.left, key);
        } else {
            return getNode(root.right, key);
        }
    }

    @Override
    public V remove(K key) {
        Node node = getNode(root, key);
        if (root != null) {
            root = remove(root, key);
            return node.value;
        }
        return null;
    }

    //删除以node为根的二分搜索树中值为e的节点，递归实现
    //返回删除后新的
    private Node remove(Node root, K key) {
        if (root == null) {
            return null;
        }
        if (key.compareTo(root.key) > 0) {
            root.right = remove(root.right, key);
            return root;
        } else if (key.compareTo(root.key) < 0) {
            root.left = remove(root.left, key);
            return root;
        } else {
            if (root.left == null) {
                Node rightNode = root.right;
                root.right = null;
                size--;
                return rightNode;
            }
            if (root.right == null) {
                Node leftNode = root.left;
                root.left = null;
                size--;
                return leftNode;
            }
            Node successor = minimum(root.right);
            successor.right = removeMin(root.right);
            successor.left = root.left;
            root.left = root.right = null;
            return successor;
        }
    }

    @Override
    public boolean contains(K key) {
        return getNode(root, key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        return node == null ? null : node.value;
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(root, key);
        if (node == null) {
            throw new IllegalArgumentException("key does not exist");
        }
        node.value = newValue;
    }

    //找出最小的节点
    private Node minimum(Node root) {
        if (root.left == null) {
            return root;
        }
        return minimum(root.left);
    }

    private Node removeMin(Node root) {
        //如果没有左孩子了，代表这个节点就是最小的了
        if (root.left == null) {
            //保存右节点
            Node rightNode = root.right;
            //删掉原先的右孩子
            root.right = null;
            size--;
            //作为上层节点的左孩子返回
            return rightNode;
        }
        root.left = removeMin(root.left);
        return root;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }
}
